﻿using DevExpress.LookAndFeel;
using DevExpress.XtraEditors;
using DevExpress.XtraNavBar;
using OTS.Utility;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace OTS
{
    public partial class MainForm : XtraForm
    {

        Dictionary<string, XForm> forms = new Dictionary<string, XForm>();

        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {

            this.KeyDown += MainForm_KeyDown;
            this.MdiChildActivate += MainForm_MdiChildActivate;

            this.Text = "Karbel Ödeme Takip Sistemi" + (AppSession.Version != null ? " - v" + AppSession.Version : "");

            bsiUser.Caption = Utility.AppSession.UserInfo.KullaniciAdi;
            UserLookAndFeel.Default.SetSkinStyle(Login.SkinName);
            this.WindowState = FormWindowState.Maximized;

            //new RibbonManager(Ribbon);

            this.Cursor = Cursors.WaitCursor;
            ShowMdiChildForm(typeof(OdemeTablosu));
            this.Cursor = Cursors.Default;

            if (!AppSession.UserInfo.Yonetici)
                nbSistem.Visible = false;

            List<int> cariGorebilir = new List<int>();
            cariGorebilir.AddRange(new int[] { 2, 14, 16 });

            if (cariGorebilir.Contains(AppSession.UserInfo.KullaniciId))
                nbCari.Visible = true;

            AppSession.MainForm = this;
        }

        private void MainForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.F12)
            {
                MessageBox.Show(AppSession.DataManager.ConnectionString);
            }
            if (RibbonManager.isDeveloper)
            {
                if (e.Control && e.KeyCode == Keys.F2)
                {

                }
            }
        }

        private void MainForm_Shown(object sender, EventArgs e)
        {
            AppSession.MainForm.ShowInTaskbar = true;
            Guncelle();
        }

        private void MainForm_MdiChildActivate(object sender, EventArgs e)
        {
            XForm frm = (XForm)this.ActiveMdiChild;
        }

        public XForm GetActiveForm(Type type, params object[] prm)
        {
            XForm frm;
            string hash = GetHash(type, prm);

            forms.TryGetValue(hash, out frm);

            if (frm != null)
                if (!frm.IsDisposed)
                {
                    frm.Activate();
                    return frm;
                }
                else
                    forms.Remove(hash);

            return null;
        }

        private string GetHash(Type type, object[] prm)
        {
            string prmHash = "";
            foreach (object o in prm)
                prmHash += o.GetHashCode();

            return type.GetHashCode() + "." + prmHash;
        }

        public XForm ActiveXForm = null;

        public void SetActiveXFormFromMDIManager()
        {
            if (mdiManager.SelectedPage == null)
                ActiveXForm = null;
            else
            {
                XForm activeForm = mdiManager.SelectedPage.MdiChild as XForm;
                ActiveXForm = activeForm;
            }
        }

        private void mdiManager_SelectedPageChanged(object sender, EventArgs e)
        {
            SetActiveXFormFromMDIManager();
        }

        public XForm ShowMdiChildForm(Type type, params object[] prm)
        {
            XForm frm = GetActiveForm(type, prm);

            if (frm != null)
            {
                ActiveXForm = frm;
                return frm;
            }

            try
            {
                if (type.BaseType == typeof(XPopupForm))
                {
                    using (XPopupForm popup = (XPopupForm)Activator.CreateInstance(type, prm))
                    {
                        //AppSession.DataManager.ExecuteNonQuery("InsLogForm", WhoAmI, popup.GetType().ToString());
                        ActiveXForm = popup;
                        frm = popup;
                        popup.ShowDialog();
                    }
                }
                else
                {
                    frm = (XForm)Activator.CreateInstance(type, prm);
                    frm.MdiParent = this;
                    frm.WindowState = FormWindowState.Maximized;
                    string hash = GetHash(type, prm);
                    forms.Add(hash, frm);
                    //AppSession.DataManager.ExecuteNonQuery("InsLogForm", WhoAmI, frm.GetType().ToString());
                    ActiveXForm = frm;
                    frm.Show();
                }
            }
            catch (Exception Ex)
            {
                XtraMessageBox.Show(Ex.InnerException.ToString());
            }
            finally
            {

            }

            return frm;
        }

        private void btnOdemeTablosu_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(OdemeTablosu));
        }

        private void nbKullanici_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(Kullanicilar));
        }

        private void btnManual_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(ManualOdeme));
        }

        private void btnAjanda_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(Ajanda));
        }

        private void nbResmiTatiller_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(ResmiTatiller));
        }

        private void btnGuncelle_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            Guncelle();
        }

        private void Guncelle()
        {
            this.Cursor = Cursors.WaitCursor;
            AppSession.DataManager.ExecuteNonQuery("JOB_Guncelle");
            XtraMessageBox.Show("Veriler güncellendi.", "Uyarı", MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.Cursor = Cursors.Default;
        }

        private void nbiBakiyeler_LinkClicked(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(Bakiyeler));
        }



        private void navBarItem1_LinkPressed(object sender, NavBarLinkEventArgs e)
        {
            ShowMdiChildForm(typeof(Test));

        }

    }
}